package com.jrx.anytxn.reporting.job.unionFile.step;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.reporting.config.UnionFileConfig;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author liuyu
 * @Description 合并文件job监听，重跑时删除已存在的合并文件
 */
@Service
public class MergeUnionFileListener implements JobExecutionListener {

	@Autowired
	private UnionFileConfig unionFileConfig;

	@Autowired
	private IOrganizationTableService organizationTableService;

	@Override
	public void beforeJob(JobExecution jobExecution) {
		// 获取文件路径
		PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(ReportingConstant.DEFAULT_ORG_NUMBER);
		String filePath = unionFileConfig.getUnionFilePath();
		Date bussinessDate = org.getToday();
		String toDay = DateUtils.format(bussinessDate, "yyyyMMdd");
		String reportingPath = filePath + org.getTenantId() + File.separator + toDay;
		Path path = Paths.get(reportingPath);
		// 删除文件
		File toFile = path.toFile();
		File[] files = toFile.listFiles();
        List<String> list = Arrays.asList(ReportingConstant.UNION_FILE_TYPE_LOAN_SERJAR,
                ReportingConstant.UNION_FILE_TYPE_CUSTOMER_INFO,
                ReportingConstant.UNION_FILE_TYPE_CREDIT_INFO,
                ReportingConstant.UNION_FILE_TYPE_USER_CONTACT,
                ReportingConstant.UNION_FILE_TYPE_LOAN_PRODUCT,
                ReportingConstant.UNION_FILE_TYPE_REPAY_SERIAL,
                ReportingConstant.UNION_FILE_TYPE_LOAN_INFO,
                ReportingConstant.UNION_FILE_TYPE_CERTIFICATE_INFO,
                ReportingConstant.UNION_FILE_TYPE_ACCRUED_SERIAL,
                ReportingConstant.UNION_FILE_TYPE_CONTRACT_INFO,
                ReportingConstant.UNION_FILE_TYPE_REPAY_PLAN,
                ReportingConstant.UNION_FILE_TYPE_ADDRESS,
                ReportingConstant.UNION_FILE_TYPE_CUSTOMER_RESOURCE,
                ReportingConstant.UNION_FILE_TYPE_TEL,
                ReportingConstant.UNION_FILE_TYPE_PERSONAL_BASIC,
                ReportingConstant.UNION_FILE_TYPE_RISK_MANAGEMENT);
		if (files != null) {
			for (File file : files) {
				String name = file.getName();
				// 只删除此job生成的文件
				if (name.endsWith(".del")){
                    name = name.replace(".del","").trim();
                    if(list.stream().anyMatch(name :: endsWith)) {
                        file.delete();
                    }
				}

				if(name.endsWith("indicate.txt")){
                    file.delete();
                }
			}
		}
	}





	@Override
	public void afterJob(JobExecution jobExecution) {

	}

}
